package com.song.ssm.controller;

import com.song.ssm.entity.R;
import com.song.ssm.entity.StatusCode;
import com.song.ssm.pojo.User;
import com.song.ssm.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2023/10/11
 * @Version 1.0
 **/
@RestController
@CrossOrigin //解决跨域问题
public class UserController {

    @Resource
    private IUserService userService;
      @GetMapping("/users")
      public R users(@RequestParam(name = "offset",defaultValue = "1")Integer offset,
                     @RequestParam(name = "pagesize",defaultValue = "3")Integer pagesize) {
          return new R(true, StatusCode.OK,"查询成功",this.userService.pageUsers(offset,pagesize));

      }

      @PostMapping("/save")
     public R save(@RequestBody User user){
         return this.userService.save(user) ?
                 new R(true,StatusCode.OK,"保存成功！"):
                 new R(false,StatusCode.ERROR,"保存失败！");
      }

    @PutMapping("/update")
    public R update(@RequestBody User user){
        return this.userService.update(user) ?
                new R(true,StatusCode.OK,"保存成功！"):
                new R(false,StatusCode.ERROR,"保存失败！");
    }

      @DeleteMapping("/delete/{id}")
      public R del(@PathVariable int id){
          return this.userService.delete(id) ?
                  new R(true,StatusCode.OK,"删除成功！"):
                  new R(false,StatusCode.ERROR,"删除失败！");
      }

      @DeleteMapping("/batch_del/{ids}")
      public R delete(@PathVariable String ids){
          return this.userService.batch_del(ids) ?
                  new R(true,StatusCode.OK,"删除成功！"):
                  new R(false,StatusCode.ERROR,"删除失败！");
      }
}
